<?php
/**
 * Created by PhpStorm.
 * User: Hk
 * Date: 2020/11/28
 * Time: 14:55
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mockery\Exception;

class Base extends Model
{
    // 数据表主键
    protected $primaryKey = 'id';
    public $timestamps = false;
    /**
     * Notes: 查询全部数据集
     * User: HK
     * Date: 2021/1/26
     * Time: 18:13
     * email: 1919194843@qq.com
     * @param $value
     * @return false|string
     */
    // 时间戳自动转换
    public function getCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * Notes: 获取全部数据
     * User: HK
     * Date: 2021/1/26
     * Time: 18:13
     * email: 1919194843@qq.com
     * @param null $where
     * @param string $order
     * @param string $sort
     * @param null $field
     * @param null $limit
     * @param null $whereIn
     * @return mixed
     */
    public function getSelect($where = null, $order = 'id', $sort = 'desc', $field = null, $limit = null, $whereIn = null)
    {

        if (isset($whereIn)) {
            $model = self::whereIn($whereIn[0], $whereIn[1])->where($where);
        } else {
            $model = self::where($where);
        }

        if (isset($order)) {
            $model = $model->orderBy($order, $sort);
        }

        if (isset($limit)) {
            $model = $model->limit($limit);
        }

        if (isset($field)) {
            $field = explode(',', $field);
            return $model->get($field)->toArray();
        }

        return $model->get()->toArray();
    }

    /**
     * Notes: 获取带分页的数据集
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param null $where
     * @param string $order
     * @param string $sort
     * @param null $field
     * @param int $limit
     * @return mixed
     */
    public function getPaginate($where = null, $order = 'id', $sort = 'desc', $field = null, $limit = 15)
    {
        $model = self::where($where);
        if (isset($order)) {
            $model = $model->orderBy($order, $sort);
        }

        if (isset($limit)) {
            $model = $model->limit($limit);
        }

        if (isset($field)) {
            $field = explode(',', $field);
            return $model->select($field)->paginate($limit)->toArray();
        }

        return $model->select()->paginate($limit)->toArray();
    }

    /**
     * Notes: 添加数据到数据库
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param $data
     * @return false
     */
    public function addInfo($data)
    {
        if (!is_array($data)) return false;
        return self::insert($data);
    }

    /**
     * Notes: 修改
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param $id
     * @param $data
     * @return mixed
     */
    public function updateById($id, $data)
    {
        return self::where(['id' => $id])->update($data);
    }

    public function updateByWhere($where, $data)
    {
        return self::where($where)->update($data);
    }
    public function updateByWherein($where,$wherein,$data)
    {
        return self::whereIn( $where, $wherein)->update($data);
    }

    /**
     * Notes: 根据ID获取单个值
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param $id
     * @param $field
     * @return mixed
     */
    public function getValueById($id, $field)
    {
        return self::where(['id' => $id])->value($field);
    }

    /**
     * Notes: 获取单个值
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param $where
     * @param $field
     * @return mixed
     */
    public function getValue($where, $field)
    {
        return self::where($where)->value($field);
    }

    /**
     * Notes: 软删除
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param $id
     * @return mixed
     */
    public function delById($id)
    {
        return self::where(['id' => $id])->update(['status' => -1]);
    }

    public function delByWhere($where)
    {
        return self::where($where)->update(['status' => -1]);
    }

    /**
     * Notes: 删除 - 真删
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param $id
     * @return mixed
     */
    public function deleteById($id)
    {
        return self::where(['id' => $id])->delete();
    }

    public function deleteAll($ids)
    {
        $ids = explode(',', $ids);
        try {
            foreach ($ids as $key => $val) {
                $this->delById($val);
            }
            return true;
        } catch (Exception $exception) {
            return false;
        }
    }

    /**
     * Notes: 根据ID获取一条数据
     * User: HK
     * Date: 2021/1/26
     * Time: 18:14
     * email: 1919194843@qq.com
     * @param $id
     * @param string $field
     * @return array
     */
    public function getInfoById($id, $field = '')
    {
        if ($field != '') {
            $field = explode(',', $field);
            $info = self::where('id', $id)->first($field);
        } else {
            $info = self::where('id', $id)->first();
        }
        if ($info) {
            return $info->toArray();
        }
        return [];
    }

    /**
     * Notes: 根据条件获取一条数据
     * User: HK
     * Date: 2021/1/26
     * Time: 18:15
     * email: 1919194843@qq.com
     * @param $where
     * @param string $field
     * @return array
     */
    public function getInfo($where, $field = '')
    {
        if ($field != '') {
            $field = explode(',', $field);
            $info = self::where($where)->first($field);
        } else {
            $info = self::where($where)->first();
        }
        if ($info) {
            return $info->toArray();
        }
        return [];
    }

}
